# This is a Makefile for my Google App Engine Python SDK project
# It is in the src directory but is designed to be in the src's parent - so it should be symlinked to there

# TODO use /usr/local/bin/googlecode_upload.py to upload the distribution files to the project site

PROJECT := SlackBuilds
SLACKBUILD := google-appengine-pysdk
VERSION := 1.7.1
BUILD := 1
CWD := $(shell pwd)
SOURCES := README doinst.sh gae.csh gae.sh $(SLACKBUILD).SlackBuild $(SLACKBUILD).info slack-desc
SBURL := ftp://slackbuilds.org/13.37/development/
DLURL := http://googleappengine.googlecode.com/files/
SRC32 := google_appengine_$(VERSION).zip
MD532 := $(firstword $(shell md5sum lib/$(SRC32)))

.PHONY: ci co tag update getlib getsbo clean build test dist scrub
.DEFAULT: build

VPATH = src

# Check-in the current code, will prompt for commit message
ci :
	svn commit src

# Get the latest code from the repository
co :
	svn checkout https://schultzters-packages.googlecode.com/svn/trunk/$(SLACKBUILD)/ src 

# Tag the current release, will be prompted for tag message
tag :
	svn copy https://schultzters-packages.googlecode.com/svn/trunk/$(SLACKBUILD) \
	 https://schultzters-packages.googlecode.com/svn/tags/$(SLACKBUILD)/release_$(VERSION)-$(BUILD) \
	 --parents

# Work on a new version, so sync to repository, get the latest SlackBuild files, and the latest tarball
# TODO test this to make sure it works then get rid of getlib and getsbo
update : 
	svn update src
	wget -P src $(SBURL)$(SLACKBUILD)/*
	wget -P lib $(DLURL)$(SRC32)

# Download the tarball
# TODO this didn't seem to work, is it working?!
getlib :
	wget -N -P lib $(DLURL)$(SRC32)

# If the code has an official or public repository get that before continuing and if anything has changed
# TODO should we just overwrite the src directory? AFTER check-out! Should we remove -N?
getsbo : 
	wget -N -P sbo ftp://slackbuilds.org/13.37/development/$(SLACKBUILD)/*

# Clean-up any messes I may have caused
clean :
	rm -rf dist/*
	rm -rf build/*

# Build the SlackBuild 
build : clean
	sed -i -e 's/^VERSION=..*/VERSION=\$${VERSION:-$(VERSION)}/' src/$(SLACKBUILD).SlackBuild
	sed -i -e 's/^VERSION=..*/VERSION="$(VERSION)"/' src/$(SLACKBUILD).info 
	sed -i -e 's/^DOWNLOAD=..*/DOWNLOAD="$(subst /,\/,$(DLURL))$(SRC32)"/' src/$(SLACKBUILD).info
	sed -i -e 's/^MD5SUM=..*/MD5SUM="$(MD532)"/' src/$(SLACKBUILD).info
	mkdir build/$(SLACKBUILD)
	cp -u $(addprefix src/,$(SOURCES)) build/$(SLACKBUILD)

# Test the application before distributing it
test : build
	cp -u build/$(SLACKBUILD)/* tmp
	ln lib/$(SRC32) tmp/$(SRC32)

# Build the SlackBuild then build the schultzter package
# This actually needs be run as root because the SlackBuild uses chown root:root but that results in build & dist files being owned by root:root
dist : build
	tar -C build -czf dist/$(SLACKBUILD).tar.gz $(SLACKBUILD)
	ln lib/$(SRC32) build/$(SLACKBUILD)/$(SRC32)
	cd build/$(SLACKBUILD); TAG=schultzter ARCH=noarch TMP=$(CWD)/tmp OUTPUT=$(CWD)/dist ./$(SLACKBUILD).SlackBuild
	chown -R eric:users --from=root:root .

# Really clean-up everything!
scrub : clean
	rm -rf sbo/*
	rm -rf src/*~
	rm -rf tmp/*
